package com.zhangye.test.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhangye.test.mapper.MoneyTypeMapper;
import com.zhangye.test.mapper.ProxyScanCountMapper;
import com.zhangye.test.model.MoneyType;
import com.zhangye.test.model.ProxyScanCount;

/**
 * 扫描次数
 * 作者： 张叶
 * 创建时间：2018年6月14日  上午10:56:53
 */
@Service
public class ProxyScanCountService {

	@Autowired
	private ProxyScanCountMapper proxyScanCountMapper;
	@Autowired
	private MoneyTypeMapper moneyTypeMapper;
	
	

	/**
	 * 查询所有
	 * 作者： 张叶
	 * 创建时间：2018年6月14日  上午10:58:02
	 */
	public List<ProxyScanCount> selectAll(){
		List<ProxyScanCount> list = proxyScanCountMapper.selectAll();
		return list;
	}
	public List<ProxyScanCount> selectByWhere(String where){
		List<ProxyScanCount> list = proxyScanCountMapper.selectByWhere(where);
		return list;
	}
	
	
	@Transactional
	public int insert(ProxyScanCount psc){
		int m = 0;
		String where = " and type = 3";
		List<MoneyType> list = moneyTypeMapper.selectByWhere(where);
		if(list.size() > 0){
			int min = list.get(0).getMinMoney();
			int max = list.get(0).getMaxMoney();
			int count = this.getRandom(min, max);
			for(int i = 0 ; i < count ; i ++){
				 m += proxyScanCountMapper.insert(psc);
			}
		}
		return m;
	}
	
	
	public List<Map<String,Object>> selectByCounts(String where){
		List<Map<String,Object>> list = proxyScanCountMapper.selectByCounts(where);
		return list;
	}

	private int getRandom(int min,int max){
		return (int) (Math.random() * (max-min) + min + 1);
	}
}
